<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-tree
            :data="folderOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--数据表-->
      <el-col :span="20" :xs="24">
        <div class="manage-header">
          <common-form inline :formLabel="formLabel" :form="listQuery">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </common-form>
        </div>

        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['pms:type:add']"
          >新增
          </el-button>
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:user:export']"
          >导出
          </el-button>
        </el-col>
        <el-popover title="列筛选" trigger="click" style="float: right;margin-right: 10px;">
          <el-checkbox-group v-model="checkedColumns" size="mini">
            <el-checkbox style="display: block;" v-for="(item,index) in columns" :key="item.label" :label="item.label"
                         @change="chickBoxGroupChange(item)"
            >{{ item.label }}
            </el-checkbox>
          </el-checkbox-group>
          <el-button size="small" slot="reference"><i class="el-icon-arrow-down el-icon-menu"></i></el-button>
        </el-popover>

        <!--数据-->
        <common-table :list="list" @handleSelectionChange="handleSelectionChange" :options="options"
                      :columns="columns" :operates="operates"
        >
        </common-table>
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                    @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listData, treeselect, getColumns } from '@/api/pms/repository'
import { delDoc } from '@/api/pms/doc'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import CommonForm from '@/views/components/CommonForm'
import CommonTable from '@/views/components/dynamicTable'

export default {
  name: 'User',
  components: { Treeselect, CommonForm, CommonTable },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 树选项
      folderOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 名称
      folderName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        folderId: undefined,
        documentName: undefined,
        code: undefined,
        title: undefined
      },
      formLabel: [
        {
          model: 'documentName',
          label: '名称',
          type: 'input'
        },
        {
          model: 'code',
          label: '编号',
          type: 'input'
        },
        {
          model: 'title',
          label: '标题',
          type: 'input'
        }
      ],
      checkedColumns: [],
      options: {// table 的参数
        stripe: true, // 是否为斑马纹 table
        loading: false, // 是否添加表格loading加载动画
        highlightCurrentRow: true, // 是否支持当前行高亮显示
        mutiSelect: true // 是否支持列表项选中功能
      },
      columns: [],
      // 按钮
      operates: {// 列操作按钮
        width: 150,
        list: [
          {
            id: '1',
            label: '删除',
            style: 'cursor:pointer;color: #409eff;',
            class: 'el-icon-delete',
            show: true,
            plain: false,
            method: (index, row) => {
              this.handleDel(row)
            }
          }
        ]
      }
    }
  },
  watch: {
    // 根据名称筛选树
    folderName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getColumn()
    this.getList()
    this.getTreeselect()
  },
  methods: {
    chickBoxGroupChange(item) {
      item.show = !item.show
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    getColumn() {
      this.loading = true
      getColumns().then(response => {
        this.columns = response.data
        this.columns.forEach((item, index) => {
          this.checkedColumns.push(item.label)
        })
        this.loading = false
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      listData(this.addDateRange(this.listQuery, this.dateRange)).then(response => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.folderOptions = response.data
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.listQuery.folderId = data.id
      this.getList()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.listQuery.page = 1
      this.listQuery.folderId = undefined
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 新增
    handleAdd() {

    },
    // 导出
    handleExport(){

    },
    /** 删除按钮操作 */
    handleDel(row) {
      const dIds = row.id || this.ids
      this.$confirm('是否确认删除编号为"' + dIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delDoc(dIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>
